/***************************************************************************
 *   Copyright (C) 2008 by Silvan Imsand <imsandli@gmail.com>              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

package com.sametime.twitterclient.preferences;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.sametime.twitterclient.Activator;
import com.sametime.twitterclient.twitterapi.TwitterData;

/**
 * @since 1.0
 * @author Silvan Imsand
 */

public class TwitterPreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	public TwitterPreferencePage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Twitter settings");
	}

	/**
	 * Creates the field editors. Field editors are abstractions of the common
	 * GUI blocks needed to manipulate various types of preferences. Each field
	 * editor knows how to save and restore itself.
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
	 */
	public void createFieldEditors() {
		// Username Field
		addField(new StringFieldEditor(PreferenceConstants.P_USERNAME,
				"&Username:", getFieldEditorParent()));

		// Password Field
		StringFieldEditor passwordField = new StringFieldEditor(
				PreferenceConstants.P_PASSWORD, "&Password",
				getFieldEditorParent());
		passwordField.getTextControl(getFieldEditorParent()).setEchoChar('*');
		addField(passwordField);

		// Show Messages of Friends
		addField(new BooleanFieldEditor(PreferenceConstants.P_SHOWFRIENDS,
				"&Show messages of friends", getFieldEditorParent()));

		// Refresh Level
		IntegerFieldEditor refreshLevel = new IntegerFieldEditor(
				PreferenceConstants.P_REFRESHLEVEL, "&Refresh in minutes",
				getFieldEditorParent(), 5);
		refreshLevel.setValidRange(2, 60);
		refreshLevel
				.setErrorMessage("Value must be an Integer between 2 and 60");
		addField(refreshLevel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}

	/**
	 * Called when user clicks Apply or OK
	 * 
	 * @return boolean
	 */
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#performOk()
	 */
	public boolean performOk() {
		// Save Preferences
		boolean ret = super.performOk();
		// Check for new data
		TwitterData.getInstance().startTimer();
		return ret;

	}
}